﻿function isky_orderForm() {
    debugger;
    var entityId = Xrm.Page.data.entity.getId();
    var new_usdcost = GetUsdcostSum(entityId);
    new_usdcost = parseFloat(new_usdcost).toFixed(2);

    if (new_usdcost == 0 || new_usdcost == "" || new_usdcost == "NaN") {
        Xrm.Page.getAttribute("new_totalprofit").setValue(0);
        Xrm.Page.getAttribute("new_totalprofitenglish").setValue("Zero");
        Xrm.Page.getAttribute("new_totalprofitcapital").setValue("零元整");
    } else {
        Xrm.Page.getAttribute("new_totalprofit").setValue(new_usdcost);
        var zhongwen = moneyArabiaToChinese(new_usdcost);
        Xrm.Page.getAttribute("new_totalprofitcapital").setValue(zhongwen);
        //var yingwen = englishmoney(new_usdcost);
        //Xrm.Page.getAttribute("new_totalprofitenglish").setValue(yingwen);
    }
    GetTime();
    Xrm.Page.getAttribute("new_time").addOnChange(function () {
        debugger;
        GetTime();
    });

    Xrm.Page.data.save();
}

function GetTime() {
    var new_time = Xrm.Page.getAttribute("new_time").getValue();
    if (new_time != null) {
        var year = new_time.getFullYear();
        var month = new_time.getMonth() + 1 < 10 ? "0" + (new_time.getMonth() + 1) : new_time.getMonth() + 1;
        var date = new_time.getDate() < 10 ? "0" + new_time.getDate() : new_time.getDate();

        var zhuangchuan = year + "年" + month + "月" + date + "日";
        Xrm.Page.getAttribute("new_shipmentdate").setValue(zhuangchuan);
    }
}

function GetUsdcostSum(entityId) {
    var fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' aggregate='true'>" +
        "<entity name='new_ygcb'>" +
        "    <attribute name='new_usdcost'  alias='usdcost' aggregate='sum'/>" +
        "    <filter type='and'>" +
        "       <condition attribute='new_case' operator='eq' value='" + entityId+"' />" +
        "    </filter>" +
        " </entity>" +
        "</fetch>";

    var retrTotalQuota = XrmServiceToolkit.Soap.Fetch(fetchXml);
    if (retrTotalQuota && retrTotalQuota.length > 0) {
        return retrTotalQuota[0].attributes.usdcost.value;
    }
    return 0;
}

function moneyArabiaToChinese(Num) {    //阿拉伯数字转中文大写格式
    var isNegative = false;// 是否负数
    if (Num < 0) {
        Num = -Num;
        isNegative = true;
    }
    if (typeof Num == 'number') {
        Num = Num.toString();
    }
    for (var i = Num.length - 1; i >= 0; i--) {
        Num = Num.replace(",", "");// 替换money中的“,”
        Num = Num.replace(" ", "");// 替换money中的空格
    }
    Num = Num.replace("￥", "");// 替换掉可能出现的￥字符
    if (isNaN(Num)) { // 验证输入的字符是否为数字
        return;
    }
    // ---字符处理完毕，开始转换，转换采用前后两部分分别转换---//
    part = String(Num).split(".");
    newchar = "";
    // 小数点前进行转化
    for (var i = part[0].length - 1; i >= 0; i--) {
        if (part[0].length > 10) {
            alertWarning("位数过大，无法计算");
            return "";
        } // 若数量超过拾亿单位，提示
        tmpnewchar = "";
        perchar = part[0].charAt(i);
        switch (perchar) {
            case "0":
                tmpnewchar = "零" + tmpnewchar;
                break;
            case "1":
                tmpnewchar = "壹" + tmpnewchar;
                break;
            case "2":
                tmpnewchar = "贰" + tmpnewchar;
                break;
            case "3":
                tmpnewchar = "叁" + tmpnewchar;
                break;
            case "4":
                tmpnewchar = "肆" + tmpnewchar;
                break;
            case "5":
                tmpnewchar = "伍" + tmpnewchar;
                break;
            case "6":
                tmpnewchar = "陆" + tmpnewchar;
                break;
            case "7":
                tmpnewchar = "柒" + tmpnewchar;
                break;
            case "8":
                tmpnewchar = "捌" + tmpnewchar;
                break;
            case "9":
                tmpnewchar = "玖" + tmpnewchar;
                break;
        }
        switch (part[0].length - i - 1) {
            case 0:
                tmpnewchar = tmpnewchar + "元";
                break;
            case 1:
                if (perchar != 0)
                    tmpnewchar = tmpnewchar + "拾";
                break;
            case 2:
                if (perchar != 0)
                    tmpnewchar = tmpnewchar + "佰";
                break;
            case 3:
                if (perchar != 0)
                    tmpnewchar = tmpnewchar + "仟";
                break;
            case 4:
                tmpnewchar = tmpnewchar + "万";
                break;
            case 5:
                if (perchar != 0)
                    tmpnewchar = tmpnewchar + "拾";
                break;
            case 6:
                if (perchar != 0)
                    tmpnewchar = tmpnewchar + "佰";
                break;
            case 7:
                if (perchar != 0)
                    tmpnewchar = tmpnewchar + "仟";
                break;
            case 8:
                tmpnewchar = tmpnewchar + "亿";
                break;
            case 9:
                tmpnewchar = tmpnewchar + "拾";
                break;
        }
        newchar = tmpnewchar + newchar;
    }
    // 小数点之后进行转化
    if (Num.indexOf(".") != -1) {
        if (part[1].length > 2) {
            alertWarning("小数点之后只能保留两位,系统将自动截段");
            part[1] = part[1].substr(0, 2);
        }
        for (var i = 0; i < part[1].length; i++) {
            tmpnewchar = "";
            perchar = part[1].charAt(i);
            switch (perchar) {
                case "0":
                    tmpnewchar = "零" + tmpnewchar;
                    break;
                case "1":
                    tmpnewchar = "壹" + tmpnewchar;
                    break;
                case "2":
                    tmpnewchar = "贰" + tmpnewchar;
                    break;
                case "3":
                    tmpnewchar = "叁" + tmpnewchar;
                    break;
                case "4":
                    tmpnewchar = "肆" + tmpnewchar;
                    break;
                case "5":
                    tmpnewchar = "伍" + tmpnewchar;
                    break;
                case "6":
                    tmpnewchar = "陆" + tmpnewchar;
                    break;
                case "7":
                    tmpnewchar = "柒" + tmpnewchar;
                    break;
                case "8":
                    tmpnewchar = "捌" + tmpnewchar;
                    break;
                case "9":
                    tmpnewchar = "玖" + tmpnewchar;
                    break;
            }
            if (i == 0)
                tmpnewchar = tmpnewchar + "角";
            if (i == 1)
                tmpnewchar = tmpnewchar + "分";
            newchar = newchar + tmpnewchar;
        }
    }
    // 替换所有无用汉字
    while (newchar.search("零零") != -1)
        newchar = newchar.replace("零零", "零");
    newchar = newchar.replace("零亿", "亿");
    newchar = newchar.replace("亿万", "亿");
    newchar = newchar.replace("零万", "万");
    newchar = newchar.replace("零元", "元");
    newchar = newchar.replace("零角", "");
    newchar = newchar.replace("零分", "");

    if (newchar.charAt(newchar.length - 1) == "元" || newchar.charAt(newchar.length - 1) == "角") {
        newchar = newchar + "整";
    }

    if (isNegative) {
        newchar = '负' + newchar;
    }

    return newchar;
}





function englishmoney(num) {
    var i;
    var hundreds;
    var tenth;
    var one;
    var thousand;
    var million;
    var billion;
    var numbers;
    var s
    var result;

    if ((num == '') || (num == null)) return ('');
    var str;
    str = '000000000000000000' + num.toString();
    numbers = 'ONE TWO THREE FOUR FIVE '
        + 'SIX SEVEN Eight NiNE TEN '
        + 'ElEVEN TWELVE THIRTEEN FOURTEEN FIFTEEN '
        + 'SIXTEEN SEVEVTEEN EIGHTEEN NINETEEN '
        + 'TWENTY THIRTY FORTY FIFTY '
        + 'SIXTY SEVEVTY EIGHTY NINETY ';
    String.prototype.Trim = function () { return this.replace(/^\s+|\s+$/g, " "); }
    String.prototype.Ltrim = function () { return this.replace(/^\s+/g, " "); }
    String.prototype.Rtrim = function () { return this.replace(/\s+$/g, " "); }
    s = str.substring(str.length - 15, str.length);
    billion = parseInt(s.substring(0, 3)); //将12位整数分成4段：十亿、百万、千、百十个
    million = parseInt(s.substring(3, 6));
    thousand = parseInt(s.substring(6, 9));
    result = ' ';
    i = 0;
    while (i <= 3) {
        hundreds = parseInt(s.substring(i * 3, i * 3 + 1));//百位0-9
        tenth = parseInt(s.substring(i * 3 + 1, i * 3 + 2));
        if (tenth == 1) { one = 10 + parseInt(s.substring(i * 3 + 2, i * 3 + 3)); }
        else { one = 0 + parseInt(s.substring(i * 3 + 2, i * 3 + 3)); }
        // one=(CASE @tenth WHEN 1 THEN 10 ELSE 0 END)+CAST(SUBSTRING(@s,@i*3+3,1) AS int)--个位0-19
        if (tenth <= 1) { tenth = 0; }

        // tenth=(CASE WHEN @tenth <=1 THEN 0 ELSE @tenth END)--十位0、2-9
        if ((i == 1 && parseInt(billion) > 0 && (parseInt(million) > 0 || parseInt(thousand) > 0 || parseInt(hundreds) > 0)) ||
            (i == 2 && (parseInt(billion) > 0 || parseInt(million) > 0) && (parseInt(thousand) > 0 || parseInt(hundreds) > 0)) ||
            (i == 3 && (parseInt(billion) > 0 || parseInt(million) > 0 || parseInt(thousand) > 0) && (parseInt(hundreds) > 0))) { result = result + ', '; }//--百位不是0则每段之间加连接符,

        if (i == 3 && (billion > 0 || million > 0 || thousand > 0) && (hundreds == 0 && (tenth > 0 || one > 0))) { result = result + ' AND '; }//--百位是0则加连接符AND

        if (hundreds > 0) { result = result + numbers.substring(hundreds * 10 - 10, hundreds * 10).Rtrim() + ' HUNDRED '; }

        if (tenth >= 2 && tenth <= 9) {
            if (hundreds > 0) { result = result + ' AND '; }
            result = result + numbers.substring(tenth * 10 + 170, tenth * 10 + 180).Rtrim();
        }

        if (one >= 1 && one <= 19) {
            if (tenth > 0) { result = result + '- '; }
            else {
                if (hundreds > 0) { result = result + ' AND '; }
            }
            result = result + numbers.substring(one * 10 - 10, one * 10).Rtrim();
        }

        if (i == 0 && parseInt(billion) > 0) {
            result = result + ' BILLION ';
        }
        if (i == 1 && parseInt(million) > 0) {
            result = result + ' MILLION ';
        }
        if (i == 2 && parseInt(thousand) > 0) {
            result = result + ' THOUSAND ';
        }
        i = i + 1;
    }
    if (s.substring(13, 15) != '00 ') {
        result = result + ' ONLY ';
        if (s.substring(13, 14) == '0 ') {
            result = result + 'ZERO ';
        }
        else { result = result + numbers.substring(parseInt(s.substring(13, 14)) * 10 - 10, parseInt(s.substring(13, 14)) * 10).Rtrim(); }
        if (s.substring(14, 15) != '0 ') {
            result = result + ' ' + numbers.substring(parseInt(s.substring(14, 15)) * 10 - 10, parseInt(s.substring(14, 15)) * 10).Rtrim();
        }
    }

    return (result);
}



//function englishmoney(num) {
//    var i;
//    var hundreds;
//    var tenth;
//    var one;
//    var thousand;
//    var million;
//    var billion;
//    var numbers;
//    var s
//    var result;

//    if ((num == '') || (num == null)) return ('');
//    var str;
//    str = '000000000000000000 '+ num.toString();
//    numbers = 'ONE TWO THREE FOUR FIVE '
//        + 'SIX SEVEN Eight NiNE TEN '
//        + 'ElEVEN TWELVE THIRTEEN FOURTEEN FIFTEEN '
//        + 'SIXTEEN SEVEVTEEN EIGHTEEN NINETEEN '
//        + 'TWENTY THIRTY FORTY FIFTY '
//        + 'SIXTY SEVEVTY EIGHTY NINETY ';
//    String.prototype.Trim = function () { return this.replace(/^\s+|\s+$/g, " "); }
//    String.prototype.Ltrim = function () { return this.replace(/^\s+/g, " "); }
//    String.prototype.Rtrim = function () { return this.replace(/\s+$/g, " "); }
//    s = str.substring(str.length - 15, str.length);
//    billion = parseInt(s.substring(0, 3)); //将12位整数分成4段：十亿、百万、千、百十个
//    million = parseInt(s.substring(3, 6));
//    thousand = parseInt(s.substring(6, 9));
//    result = ' ';
//    i = 0;
//    while (i <= 3) {
//        hundreds = parseInt(s.substring(i * 3, i * 3 + 1));//百位0-9
//        tenth = parseInt(s.substring(i * 3 + 1, i * 3 + 2));
//        if (tenth == 1) { one = 10 + parseInt(s.substring(i * 3 + 2, i * 3 + 3)); }
//        else { one = 0 + parseInt(s.substring(i * 3 + 2, i * 3 + 3)); }
//        // one=(CASE @tenth WHEN 1 THEN 10 ELSE 0 END)+CAST(SUBSTRING(@s,@i*3+3,1) AS int)--个位0-19
//        if (tenth <= 1) { tenth = 0; }

//        // tenth=(CASE WHEN @tenth <=1 THEN 0 ELSE @tenth END)--十位0、2-9
//        if ((i == 1 && parseInt(billion) > 0 && (parseInt(million) > 0 || parseInt(thousand) > 0 || parseInt(hundreds) > 0)) ||
//            (i == 2 && (parseInt(billion) > 0 || parseInt(million) > 0) && (parseInt(thousand) > 0 || parseInt(hundreds) > 0)) ||
//            (i == 3 && (parseInt(billion) > 0 || parseInt(million) > 0 || parseInt(thousand) > 0) && (parseInt(hundreds) > 0))) { result = result + ', '; }//--百位不是0则每段之间加连接符,

//        if (i == 3 && (billion > 0 || million > 0 || thousand > 0) && (hundreds == 0 && (tenth > 0 || one > 0))) { result = result + ' AND '; }//--百位是0则加连接符AND

//        if (hundreds > 0) { result = result + numbers.substring(hundreds * 10 - 10, hundreds * 10).Rtrim() + ' HUNDRED '; }

//        if (tenth >= 2 && tenth <= 9) {
//            if (hundreds > 0) { result = result + ' AND '; }
//            result = result + numbers.substring(tenth * 10 + 170, tenth * 10 + 180).Rtrim();
//        }

//        if (one >= 1 && one <= 19) {
//            if (tenth > 0) { result = result + '- '; }
//            else {
//                if (hundreds > 0) { result = result + ' AND '; }
//            }
//            result = result + numbers.substring(one * 10 - 10, one * 10).Rtrim();
//        }

//        if (i == 0 && parseInt(billion) > 0) {
//            result = result + ' BILLION ';
//        }
//        if (i == 1 && parseInt(million) > 0) {
//            result = result + ' MILLION ';
//        }
//        if (i == 2 && parseInt(thousand) > 0) {
//            result = result + ' THOUSAND ';
//        }
//        i = i + 1;
//    }
//    if (s.substring(13, 15) != '00 ') {
//        result = result + ' ONLY ';
//        if (s.substring(13, 14) == '0 ') {
//            result = result + 'ZERO ';
//        }
//        else { result = result + numbers.substring(parseInt(s.substring(13, 14)) * 10 - 10, parseInt(s.substring(13, 14)) * 10).Rtrim(); }
//        if (s.substring(14, 15) != '0 ') {
//            result = result + ' ' + numbers.substring(parseInt(s.substring(14, 15)) * 10 - 10, parseInt(s.substring(14, 15)) * 10).Rtrim();
//        }
//    }

//    return (result);
//}












 function dateToString(date) {
    var year = date.getFullYear();
    var month = (date.getMonth() + 1).toString();
    var day = (date.getDate()).toString();
    if (month.length == 1) {
        month = "0" + month;
    }
    if (day.length == 1) {
        day = "0" + day;
    }
    var dateTime = year + "-" + month + "-" + day;
    return dateTime;
}






//function englishmoney() {
//    var i;
//    var hundreds;
//    var tenth;
//    var one;
//    var thousand;
//    var million;
//    var billion;
//    var numbers;
//    var s
//    var result;
//    var num;
//    num = document.all['usd'].value;
//    var str;
//    str = '000000000000000' + num.toString();
//    numbers = 'one       two       three     four      five      '
//        + 'six       seven     eight     nine      ten       '
//        + 'eleven    twelve    thirteen  fourteen  fifteen   '
//        + 'sixteen   seventeen eighteen  nineteen  '
//        + 'twenty    thirty    forty     fifty     '
//        + 'sixty     seventy   eighty    ninety    ';
//    String.prototype.Trim = function () { return this.replace(/^\s+|\s+$/g, ""); }
//    String.prototype.Ltrim = function () { return this.replace(/^\s+/g, ""); }
//    String.prototype.Rtrim = function () { return this.replace(/\s+$/g, ""); }
//    s = str.substring(str.length - 15, str.length);
//    billion = parseInt(s.substring(0, 3)); //将12位整数分成4段：十亿、百万、千、百十个
//    million = parseInt(s.substring(3, 6));
//    thousand = parseInt(s.substring(6, 9));
//    result = '';
//    i = 0;


//    while (i <= 3) {
//        hundreds = parseInt(s.substring(i * 3, i * 3 + 1));//百位0-9
//        tenth = parseInt(s.substring(i * 3 + 1, i * 3 + 2));
//        if (tenth == 1) { one = 10 + parseInt(s.substring(i * 3 + 2, i * 3 + 3)); }
//        else { one = 0 + parseInt(s.substring(i * 3 + 2, i * 3 + 3)); }
//        // one=(CASE @tenth WHEN 1 THEN 10 ELSE 0 END)+CAST(SUBSTRING(@s,@i*3+3,1) AS int)--个位0-19
//        if (tenth <= 1) { tenth = 0; }

//        // tenth=(CASE WHEN @tenth<=1 THEN 0 ELSE @tenth END)--十位0、2-9
//        if ((i == 1 && parseInt(billion) > 0 && (parseInt(million) > 0 || parseInt(thousand) > 0 || parseInt(hundreds) > 0)) ||
//            (i == 2 && (parseInt(billion) > 0 || parseInt(million) > 0) && (parseInt(thousand) > 0 || parseInt(hundreds) > 0)) ||
//            (i == 3 && (parseInt(billion) > 0 || parseInt(million) > 0 || parseInt(thousand) > 0) && (parseInt(hundreds) > 0))) { result = result + ', '; }//--百位不是0则每段之间加连接符,

//        if (i == 3 && (billion > 0 || million > 0 || thousand > 0) && (hundreds == 0 && (tenth > 0 || one > 0))) { result = result + ' and '; }//--百位是0则加连接符AND

//        if (hundreds > 0) { result = result + numbers.substring(hundreds * 10 - 10, hundreds * 10).Rtrim() + ' hundred'; }

//        if (tenth >= 2 && tenth <= 9) {
//            if (hundreds > 0) { result = result + ' and '; }
//            result = result + numbers.substring(tenth * 10 + 170, tenth * 10 + 180).Rtrim();
//        }

//        if (one >= 1 && one <= 19) {
//            if (tenth > 0) { result = result + '-'; }
//            else {
//                if (hundreds > 0) { result = result + ' and '; }
//            }
//            result = result + numbers.substring(one * 10 - 10, one * 10).Rtrim();
//        }
//        //alert(result);
//        // i=i+1;
//        // } 
//        if (i == 0 && parseInt(billion) > 0) {
//            result = result + ' billion';
//        }
//        if (i == 1 && parseInt(million) > 0) {
//            result = result + ' million';
//        }
//        if (i == 2 && parseInt(thousand) > 0) {
//            result = result + ' thousand';
//        }
//        i = i + 1;
//    }
//    if (s.substring(13, 15) != '00') {
//        result = result + ' point ';
//        if (s.substring(13, 14) == '0') {
//            result = result + 'zero';
//        }
//        else { result = result + numbers.substring(parseInt(s.substring(13, 14)) * 10 - 10, parseInt(s.substring(13, 14)) * 10).Rtrim(); }
//        if (s.substring(14, 15) != '0') {
//            result = result + ' ' + numbers.substring(parseInt(s.substring(14, 15)) * 10 - 10, parseInt(s.substring(14, 15)) * 10).Rtrim();
//        }
//    }
//    document.all['dollars'].innerText = result;
//}